package com.jwthhealth_pub.old.heartscreen.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.heartscreen.module.HeartScreenModule;
import com.jwthhealth_pub.old.heartscreen.presenter.IHeartScreenPresenter;
import com.jwthhealth_pub.old.heartscreen.presenter.IHeartScreenPresenterCompl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/1/17.
 */
public class HeartScreenActivity extends Activity {

    @BindView(R.id.radiogroup)
    RadioGroup radiogroup;
    @BindView(R.id.ed_heartrhythm)
    EditText edHeartrhythm;
    @BindView(R.id.ed_hearttime_minute)
    EditText edHearttimeMinute;
    @BindView(R.id.ed_hearttime_second)
    EditText edHearttimeSecond;
    @BindView(R.id.ed_age)
    EditText edAge;
    @BindView(R.id.ed_weight)
    EditText edWeight;
    @BindView(R.id.btn_save)
    Button btnCommit;
    @BindView(R.id.run)
    RadioButton run;
    @BindView(R.id.walk)
    RadioButton walk;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sp_gender)
    Spinner spGender;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    @BindView(R.id.empty_view)
    View eimptyView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.include_info)
    View infoIncludeView;
    private IHeartScreenPresenter iHeartScreenPresenter;
    private String type = "-1";//运动种类
    private String gender = "-1";
    boolean hasScroll = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_heartscreen);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        iHeartScreenPresenter = new IHeartScreenPresenterCompl(this);

        initTop();
        initListener();
    }

    private void initListener() {
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                scrollToBottom();
                if (i == run.getId()) {
                    type = "2";
                } else {
                    type = "1";
                }
            }
        });

        spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                scrollToBottom();
                String[] languages = getResources().getStringArray(R.array.languages);
                if (i == 0) {
                    gender = "2";
                } else {
                    gender = "1";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
//                    scrollToBottom();
                }
            }
        };
        edHeartrhythm.setOnFocusChangeListener(onFocusChangeListener);
        edHearttimeMinute.setOnFocusChangeListener(onFocusChangeListener);
        edHearttimeSecond.setOnFocusChangeListener(onFocusChangeListener);
        edAge.setOnFocusChangeListener(onFocusChangeListener);
        edWeight.setOnFocusChangeListener(onFocusChangeListener);
    }

    private void initTop() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick(R.id.btn_save)
    public void onClick() {
        if (type == "-1") {
            Toast.makeText(this, "请选择运动种类", Toast.LENGTH_SHORT).show();
            return;
        }

        String heartRythem = edHeartrhythm.getText().toString();
        if (heartRythem.length() <= 0) {
            Toast.makeText(this, "请输入心率", Toast.LENGTH_SHORT).show();
            return;
        }
        int lengthHeart = Integer.parseInt(heartRythem);
        if (lengthHeart <= 0 || !(lengthHeart > 40 && lengthHeart <= 220)) {
            Toast.makeText(this, "心率范围 40-220", Toast.LENGTH_SHORT).show();
            return;
        }

        String minute = edHearttimeMinute.getText().toString().trim();
        if (minute.length() <= 0) {
            Toast.makeText(this, "请输入分钟用时", Toast.LENGTH_SHORT).show();
            return;
        }
        int minuteNum = Integer.parseInt(minute);
        if (minuteNum <= 3 || minuteNum >=25) {
            Toast.makeText(this, "分钟数范围 3-25 分钟", Toast.LENGTH_SHORT).show();
            return;
        }
        String second = edHearttimeSecond.getText().toString().trim();
//        if (minute.length() <= 0) {
//            Toast.makeText(this, "请秒钟用时", Toast.LENGTH_SHORT).show();
//            return;
//        }
        if (second.length() > 0) {
            int secondNum = Integer.parseInt(second);
            if (secondNum<0 || secondNum > 59) {
                Toast.makeText(this, "请输入正确的秒数", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        int time = 0;

        if (second.length() > 0) {
            if (minute.length() > 0) {
                time = Integer.parseInt(minute) * 60 + Integer.parseInt(second);
            }
        } else {
            if (minute.length() > 0) {
                time = Integer.parseInt(minute) * 60;
            }
        }

//        gender = edGender.getText().toString();
        if (gender.trim().length() <= 0) {
            Toast.makeText(this, "请输入性别", Toast.LENGTH_SHORT).show();
            return;
        }

        String age = edAge.getText().toString();
        if (age.length() <= 0) {
            Toast.makeText(this, "请输入年龄", Toast.LENGTH_SHORT).show();
            return;
        }
        int ageNum = Integer.parseInt(age);
        if (ageNum<15 || ageNum>99) {
            Toast.makeText(this, "年龄范围 15-99 岁", Toast.LENGTH_SHORT).show();
            return;
        }

        String weight = edWeight.getText().toString();
        if (weight.length() <= 0) {
            Toast.makeText(this, "请输入体重", Toast.LENGTH_SHORT).show();
            return;
        }
        int weightNum = Integer.parseInt(weight);
        if (weightNum<30 || weightNum>99) {
            Toast.makeText(this, "体重范围 30-99 kg", Toast.LENGTH_SHORT).show();
            return;
        }

        progressbar();

//        int _age = Integer.parseInt(age);
//        if (_age < 15 && _age > 80) {
//            Toast.makeText(this, "年龄超出范围", Toast.LENGTH_SHORT).show();
//            return;
//        }

//        int _heartTythem = Integer.parseInt(heartRythem);
//        if (_heartTythem < 50 && _heartTythem > 210) {
//            Toast.makeText(this, "心率超出范围", Toast.LENGTH_SHORT).show();
//        }

        iHeartScreenPresenter.commit(weight, heartRythem, age, type, gender, String.valueOf(time));
    }

    public void progressbar() {
        if (progressbar.getVisibility() == View.VISIBLE) {
            progressbar.setVisibility(View.GONE);
        } else {
            progressbar.setVisibility(View.VISIBLE);
        }
    }

    public void scrollToBottom(){
        if (!hasScroll) {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            hasScroll = true;
        }
    }

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                progressbar();
                HeartScreenModule module = (HeartScreenModule) msg.getData().getSerializable(Constant.HEARTSCREENINGDATA);
                Intent intent = new Intent(HeartScreenActivity.this, HeartSceenAnswerActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constant.HEARTSCREENINGDATA, module);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        }
    };

//    @OnClick(R.id.info_btn)
//    public void onViewClicked() {
//        infoDialog();
//    }

//    private void infoDialog(){
//        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
//
//        //  载入布局
//        LayoutInflater inflater = getLayoutInflater();
//        ViewHolder layout = inflater.inflate(R.layout.item_heartscreen_dialog,null);
//        builder.setView(layout);
//        final AlertDialog alertDialog = builder.create();
//
//        //  对布局中的控件监听
//        RadioGroup alertRadioGroup = (RadioGroup) layout.findViewById(R.id.radiogroup);
//        final RadioButton run = (RadioButton) layout.findViewById(R.id.run);
//        final RadioButton walk = (RadioButton) layout.findViewById(R.id.walk);
//        Spinner spGender = (Spinner) layout.findViewById(R.id.sp_gender);
//        final EditText edHeartrhythm = (EditText) layout.findViewById(R.id.ed_heartrhythm);
//        final EditText edHearttimeMinute = (EditText) layout.findViewById(R.id.ed_hearttime_minute);
//        final EditText edHearttimeSecond = (EditText) layout.findViewById(R.id.ed_hearttime_second);
//        final EditText edAge = (EditText) layout.findViewById(R.id.ed_age);
//        final EditText edWeight = (EditText) layout.findViewById(R.id.ed_weight);
//        Button commitBtn = (Button) layout.findViewById(R.id.btn_commit);
//
//        alertRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
//                if (i == run.getId()) {
//                    type = "2";
//                } else {
//                    type = "1";
//                }
//            }
//        });
//
//        spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, ViewHolder view, int i, long l) {
//                String[] languages = getResources().getStringArray(R.array.languages);
//                if (i == 0) {
//                    gender = "2";
//                } else {
//                    gender = "1";
//                }
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//
//            }
//        });
//
//        commitBtn.setOnClickListener(new ViewHolder.OnClickListener() {
//            @Override
//            public void onClick(ViewHolder view) {
//                if (type == "-1") {
//                    Toast.makeText(HeartScreenActivity.this, "请选择运动种类", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                String heartRythem = edHeartrhythm.getText().toString();
//                if (heartRythem.trim().length() <= 0) {
//                    Toast.makeText(HeartScreenActivity.this, "输入正确心率", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                String minute = edHearttimeMinute.getText().toString();
//                String second = edHearttimeSecond.getText().toString();
//                int time = Integer.parseInt(minute) * 60 + Integer.parseInt(second);
//                if (second.trim().length() <= 0 || second.toString().trim().length() <= 0) {
//                    Toast.makeText(HeartScreenActivity.this, "请输入正确时间", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//
////        gender = edGender.getText().toString();
//                if (gender.trim().length() <= 0) {
//                    Toast.makeText(HeartScreenActivity.this, "请输入性别", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                String age = edAge.getText().toString();
//                if (age.trim().length() <= 0) {
//                    Toast.makeText(HeartScreenActivity.this, "输入年龄", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                String weight = edWeight.getText().toString();
//                if (weight.trim().length() <= 0) {
//                    Toast.makeText(HeartScreenActivity.this, "输入体重", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                progressbar();
//
//                int _weight = Integer.parseInt(weight);
//                if (_weight < 1 && _weight > 350) {
//                    Toast.makeText(HeartScreenActivity.this, "体重超出超出范围", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                int _age = Integer.parseInt(age);
//                if (_age < 15 && _age > 80) {
//                    Toast.makeText(HeartScreenActivity.this, "年龄超出范围", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                int _heartTythem = Integer.parseInt(heartRythem);
//                if (_heartTythem < 50 && _heartTythem > 210) {
//                    Toast.makeText(HeartScreenActivity.this, "心率超出范围", Toast.LENGTH_SHORT).show();
//                }
//
//                iHeartScreenPresenter.commit(weight, heartRythem, age, type, gender, String.valueOf(time));
//                alertDialog.dismiss();
//            }
//        });
//
//        //  显示
//        alertDialog.show();
//    }
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
//            scrollView.fullScroll(ScrollView.FOCUS_UP);
        }
}
